package com.amap.map2d.demo.overlay;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.Gradient;
import com.amap.api.maps.model.HeatmapTileProvider;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.TileOverlayOptions;
import com.amap.map2d.demo.R;
import com.amap.map2d.demo.util.SelectUtil;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * AMapV2地图中简单介绍一些Marker的用法.
 */
public class MarkerClickActivity extends Activity {

    private List<LatLng> list = new ArrayList<>();
    private MapView mMapView;
    private AMap mAMap;
    private static final int[] ALT_HEATMAP_GRADIENT_COLORS = {
            Color.argb(0, 0, 255, 255),
            Color.argb(255 / 3 * 2, 0, 255, 0),
            Color.rgb(125, 191, 0),
            Color.rgb(185, 71, 0),
            Color.rgb(255, 0, 0)
    };

    public static final float[] ALT_HEATMAP_GRADIENT_START_POINTS = {0.0f,
            0.10f, 0.20f, 0.60f, 1.0f};

    public static final Gradient ALT_HEATMAP_GRADIENT = new Gradient(
            ALT_HEATMAP_GRADIENT_COLORS, ALT_HEATMAP_GRADIENT_START_POINTS);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reli_activity);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        mAMap = mMapView.getMap();
        initDataAndHeatMap();
    }

    private void initDataAndHeatMap() {
        Toast.makeText(getApplicationContext(), "坐标点加载中...", Toast.LENGTH_LONG).show();
        new Thread() {
            @Override
            public void run() {

                List<String> c = SelectUtil.query(getApplicationContext(), "b");
                for (String temp:c){
                    String[] split = temp.split(",");
                    double a = Integer.parseInt(split[1]) / 1000000.0;
                    double b = Integer.parseInt(split[0]) / 1000000.0;
                    list.add(new LatLng(a, b));
                }
                hand.sendMessage(new Message());

            }
        }.start();


    }


    Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {
                HeatmapTileProvider heatmapTileProvider = new HeatmapTileProvider.Builder()
                        .data(list).gradient(ALT_HEATMAP_GRADIENT)
                        .build();
                mAMap.addTileOverlay(new TileOverlayOptions().tileProvider(heatmapTileProvider));

                CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(list.get(0), 8, 0, 30));
                mAMap.moveCamera(cameraUpdate);//地图移向指定区域
            }catch (Exception e){
                Toast.makeText(getApplicationContext(),"无数据，或者数据异常",Toast.LENGTH_LONG).show();
            }

        }
    };


    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
}